#ifndef XMPPCLIENT_H
#define XMPPCLIENT_H

#include <QObject>
#include <QList>
#include <gloox/client.h>
#include <gloox/connectionlistener.h>
#include <gloox/jid.h>
#include "xmppclientextension.h"

using namespace gloox;

// Implements connection listener interface, used for connect/disconnect to/from server
class XMPPClient : public QObject,
                          ConnectionListener
{
    Q_OBJECT
protected:
    QList<XMPPClientExtension *> m_extensions;
    Client  *m_client;

    // Implements gloox::ConnectionListener
    virtual void onConnect();
    virtual void onDisconnect( ConnectionError e );
    virtual bool onTLSConnect( const CertInfo& info );

public:
    // Constructors and destructor.
    XMPPClient(const JID& jid, const QString& password, int port = -1);
    XMPPClient(const QString& server);
    virtual ~XMPPClient(void);

    // Connection functions.
    void connect(void);
    void disconnect(void);

    // Connection status.
    bool authed() const;
    ConnectionState state() const;

    // Attach or detach client extensions.
    void attach(XMPPClientExtension* ext);
    void detach(XMPPClientExtension* ext);
    XMPPClient& operator+=(XMPPClientExtension *ext) { attach(ext); return *this; }
    XMPPClient& operator-=(XMPPClientExtension *ext) { detach(ext); return *this; }

    // Proxies settings goes here.
    void setHTTPProxy(const QString& host, int port, const QString& user, const QString& pass);
    void setSOCKS5Proxy(const QString& host, int port, const QString& user, const QString& pass);

    // Roster manager
    RosterManager* rosterManager();

    // Other
    gloox::JID jid();

signals:
    // Connection Established
    void connected();
    // Disconnected
    void disconnected(ConnectionError e);

private:
    // autorecieve data from socket
    void timerEvent(QTimerEvent */*event*/) { m_client->recv(0); }

    // timer id (used for autorecieve data from socket)
    int m_recvTimer;
};

#endif // XMPPCLIENT_H
